// pages/home/home.js
import {
  showText
} from "../../utils/util"
import {
  editgemomemberuser,getgemomemberuser
} from '@/api/my'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,

    radio: 0,
    firstName: '',
    firstNameState: true,
    lastName: '',
    lastNameState: true,
    dateTime: '',
    email: '',
    emailState: true,
    area: '',
    areaState: true,
    acsel1: false,
    phone:'',
    canEditBirthday:true,

    // 省市区
    regionArr: ['上海市', '上海市', '青浦区'],
    regionStr:''
  },
  bindRegionChange(e){
    console.log(this.data.regionArr)
    let {value} = e.detail
    this.setData({
      regionArr:value,
      regionStr: value.join(' ')
    })
  },
  radiosel(e) {
    console.log()
    this.setData({
      radio: e.currentTarget.dataset.nums
    })
  },
  firstNameInput(e) {
    this.setData({
      firstName: e.detail.value,
      phoneState: true
    })
  },
  lastNameInput(e) {
    this.setData({
      lastName: e.detail.value,
      lastNameState: true
    })
  },
  bindDateChange(e) {
    console.log(e.detail.value, 222)
    this.setData({
      dateTime: e.detail.value
    })
  },
  emailInput(e) {
    this.setData({
      email: e.detail.value,
      emailState: true
    })
  },
  areaInput(e) {
    this.setData({
      area: e.detail.value,
      areaState: true
    })
  },
  selone() {
    this.setData({
      acsel1: !this.data.acsel1
    })
  },
  register() {
    if (!this.data.radio) {
      showText('请选择称谓');
      return false;
    }
    if (!this.data.firstName) {
      showText('请输入姓')
      return false;
    }
    if (!this.data.lastName) {
      showText('请输入名')
      return false;
    }
    if (!this.data.dateTime) {
      showText('请选择生日')
      return false;
    }
    if (!this.data.email) {
      showText('请输入电子邮箱')
      return false;
    }
    if (!this.data.regionStr) {
      showText('请输入地区')
      return false;
    }

    let gender = '';
    if(this.data.radio == 1){
      gender = '男';
    }else{
      gender = '女';
    }
    let params = {
      birthday: this.data.dateTime,
      email: this.data.email,
      familyName: this.data.firstName,
      gender: gender,
      givenName: this.data.lastName,
      area:this.data.regionStr,
    };
    editgemomemberuser(params).then(res=>{
      if(res.code == 200){
        getgemomemberuser({}).then(res=>{
          if(res.code == 200){
            if(res.data.phonenumber){
              wx.setStorageSync('getgemomemberuser', res.data);

              wx.showModal({
                title: '提示',
                content: "提交成功",
                showCancel: false,
                success (res) {
                  wx.navigateBack();
                }
              });
            }
          }
        });

      }
    });
  },
  back() {
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getgemomemberuser({}).then(res=>{
      console.log(res);
      if(res.data.familyName){
        var firstNameState = true;
      }else{
        var firstNameState = false;
      }

      if(res.data.givenName){
        var lastNameState = true;
      }else{
        var lastNameState = false;
      }

      if(res.data.email){
        var emailState = true;
      }else{
        var emailState = false;
      }

      if(res.data.gender == "男"){
        var radio = 1;
      }else{
        var radio = 2;
      }
      var area = res.data.area;
      if(area){
        var regionArr = area.split(" ");
      }

      let phonenumber = res.data.phonenumber;
      phonenumber = phonenumber.substr(0, 3)+'****'+phonenumber.substr(7);
      this.setData({
        radio: radio,
        firstName: res.data.familyName,
        firstNameState: firstNameState,
        lastName: res.data.givenName,
        lastNameState: lastNameState,
        dateTime: res.data.birthday,
        email: res.data.email,
        emailState: emailState,
        phone:phonenumber,
        canEditBirthday:res.data.canEditBirthday,
        regionStr:res.data.area,
        regionArr:regionArr,
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})